package it.polimi.reTravelDream.beans;



import java.util.ArrayList;
import java.util.List;

import it.polimi.reTravelDream.dto.ExcursionDTO;
import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.PrenotationDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.eao.ExcursionEAOLocal;
import it.polimi.reTravelDream.eao.PrenotationEAOLocal;
import it.polimi.reTravelDream.eao.TravelPackageActionEAOLocal;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;



@ManagedBean(name="excursion")
@ViewScoped
public class ExcursionBean {
	
	@EJB
	private ExcursionEAOLocal excursionAction;
	
	@EJB
	private TravelPackageActionEAOLocal travelPackageActionEAOLocal;
	
	@EJB
	private PrenotationEAOLocal prenotationEAOLocal;
	
	
	private List<ExcursionDTO> allExcursionList;
	
	private ExcursionDTO currentExcursion;
	
	private String errorId;
	private String errorName;
	private String errorDescription;
	private String errorPrice;
	
	private String confirmCreation;
	
	private String errorDelete;
	private String errorEdit;
	
	private boolean isEdit;
	private boolean isShowError;
	
	public ExcursionBean(){
		this.currentExcursion=new ExcursionDTO();
		this.allExcursionList=new ArrayList<ExcursionDTO>();
	}
	
	@PostConstruct
	public void allExcursion(){
		this.allExcursionList=this.excursionAction.getAllExcursion();
	}
	
	public void addExcursion(){
		if (!this.check()) {
			return;
		}
		try {
			this.currentExcursion.setActive(true);
			this.excursionAction.addExcursion(this.currentExcursion);
			this.currentExcursion=new ExcursionDTO();
			this.confirmCreation="excursion created!";
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	public void edit(ExcursionDTO excursionToEdit){
		this.isEdit=true;
		
		this.errorDelete="";
		this.errorDescription="";
		this.errorEdit="";
		this.errorId="";
		this.errorName="";
		this.errorPrice="";
		
		this.isShowError=false;
		this.errorDelete="";
		this.currentExcursion=excursionToEdit;
		
	}
	
	public void saveEdit(){
		this.isShowError=false;
		this.errorDelete="";
		this.errorDescription="";
		this.errorEdit="";
		this.errorId="";
		this.errorName="";
		this.errorPrice="";
			
		
		boolean value=true;
		
		
		if (!this.checkIfPresentInAPAckage()) {
			this.errorEdit="the Excursion "+this.currentExcursion.getId()+" is part of a package,"
					+ "if you want to modify it from the system you must first remove it from"
					+ " the package. Otherwise you can disable the item "
					+ "preventing it"
					+ "to be used again (you can re enabled it later if you want)";
			value=false;
			this.isShowError=true;
			
		}
		if (!this.checkIfPresentInAPrenotation()) {
			this.errorEdit="the Excursion "+this.currentExcursion.getId()+" is part of a prenotation,"
					+ "you cannot modify";
			value=false;
			this.isShowError=true;
		}
		
		
		
		if (!value) {
			return;
		}
		
		if (!this.checkWithNoId()) {
			return;
		}
		
		try {
			this.excursionAction.edit(this.currentExcursion);
			this.allExcursionList=this.excursionAction.getAllExcursion();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void remove(ExcursionDTO excursionToRemove){
		this.isShowError=false;
		this.errorDelete="";
		this.currentExcursion=excursionToRemove;
		boolean value=true;
		
		if (!this.checkIfPresentInAPAckage()) {
			this.errorDelete="the flight "+excursionToRemove.getId()+" is part of a package,"
					+ "if you want to delete it from the system you must first remove it from"
					+ " the package and then you can delete it from the system. Otherwise you can disable the item "
					+ "preventing it"
					+ "to be used again (you can re enabled it later if you want)";
			value=false;
			this.isShowError=true;
			
		}
		if (!this.checkIfPresentInAPrenotation()) {
			this.errorDelete="the flight "+excursionToRemove.getId()+" is part of a prenotation,"
					+ "you cannot delete it from the system. You can disable the item "
					+ "preventing it"
					+ "to be used again (you can re enabled it later if you want)";
			value=false;
			this.isShowError=true;
		}
		if (!value) {
			return;
		}
		
		try {
			this.excursionAction.delete(excursionToRemove);
			this.allExcursionList=this.excursionAction.getAllExcursion();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private boolean checkIfPresentInAPAckage(){
		List<TravelPackageDTO> allTravelPackageDTOs=this.travelPackageActionEAOLocal.getAllPackages();	
		
		for(TravelPackageDTO tDto:allTravelPackageDTOs){
			List<ExcursionDTO> excursionInThePAckage=tDto.getPackageExcursions();
						
			for(ExcursionDTO excursionDTO:excursionInThePAckage){				
					if (this.currentExcursion.getId()==excursionDTO.getId()) {
						return false;					
					}
				}
		}
		return true;
	}
	
	private boolean checkIfPresentInAPrenotation(){
		List<PrenotationDTO> prenotationDTOs=this.prenotationEAOLocal.getAllPrenotation();
		for(PrenotationDTO prenotationDTO:prenotationDTOs){
			if (this.currentExcursion.getId()==prenotationDTO.getExcursioneChosen().getId()) {
				return false;
			}

		}
		return true;
	}
	
	public void cancel(){
		this.isEdit=false;
		this.isShowError=false;
	}

	
	private boolean check(){
		boolean value=true;
		this.confirmCreation="";

		this.errorId="";
		this.errorName="";
		this.errorDescription="";
		this.errorPrice="";
		
		if (!this.checkId()) {
			value=false;
			this.errorId="the id already exist";
		}
		
		if (!this.checkName()) {
			value=false;
		}
		if (!this.checkDescription()) {
			value=false;
			this.errorDescription="the description can not be empty";
		}
		if (!this.checkPrice()) {
			value=false;
			this.errorPrice="the price is not valid";
		}
		return value;
	}
	
	private boolean checkWithNoId(){
		boolean value=true;

		this.errorName="";
		this.errorDescription="";
		this.errorPrice="";
		
		if (!this.checkName()) {
			value=false;
		}
		if (!this.checkDescription()) {
			value=false;
			this.errorDescription="the description can not be empty";
		}
		if (!this.checkPrice()) {
			value=false;
			this.errorPrice="the price is not valid";
		}
		return value;
	}
	
	private boolean checkId(){
		List<ExcursionDTO> excursionDTOs=this.excursionAction.getAllExcursion();
		for(ExcursionDTO eDto:excursionDTOs){
			if (this.currentExcursion.getId()==eDto.getId()) {
				return false;
			}
		}
		return true;
	}
	private boolean checkName(){
		List<ExcursionDTO> excursionDTOs=this.excursionAction.getAllExcursion();
		for(ExcursionDTO eDto:excursionDTOs){
			if (this.currentExcursion.getName().equals(eDto.getName())) {
				this.errorName="the name already exist";
				return false;
			}
		}
		if (this.currentExcursion.getName().isEmpty()) {
			this.errorName="can not be empty";
			return false;
		}
		return true;
		
	}
	private boolean checkDescription(){
		if (this.currentExcursion.getDescription().isEmpty()) {
			return false;
		}
		return true;
	}
	private boolean checkPrice(){
		if (this.currentExcursion.getPrice()==0) {
			return false;
		}
		return true;
	}

	public void activate(ExcursionDTO excursionToActivate){
		excursionToActivate.setActive(true);
		try {
			this.excursionAction.edit(excursionToActivate);
			this.allExcursionList=this.excursionAction.getAllExcursion();
		} catch (Exception e) {
			
		}
		
	}
	
	public void disable(ExcursionDTO excursionToDisable){
		excursionToDisable.setActive(false);
		try {
			this.excursionAction.edit(excursionToDisable);
			this.allExcursionList=this.excursionAction.getAllExcursion();
		} catch (Exception e) {
			
		}
		
	}
	
	/**
	 * 
	 * getter and setter
	 */
	
	public ExcursionDTO getCurrentExcursion() {
		return currentExcursion;
	}

	public void setCurrentExcursion(ExcursionDTO currentExcursion) {
		this.currentExcursion = currentExcursion;
	}
	


	public List<ExcursionDTO> getAllExcursionList() {
		return allExcursionList;
	}

	public void setAllExcursionList(List<ExcursionDTO> allExcursionList) {
		this.allExcursionList = allExcursionList;
	}

	public String getErrorId() {
		return errorId;
	}

	public void setErrorId(String errorId) {
		this.errorId = errorId;
	}

	public String getErrorName() {
		return errorName;
	}

	public void setErrorName(String errorName) {
		this.errorName = errorName;
	}

	public String getErrorDescription() {
		return errorDescription;
	}

	public void setErrorDescription(String errorDescription) {
		this.errorDescription = errorDescription;
	}

	public String getErrorPrice() {
		return errorPrice;
	}

	public void setErrorPrice(String errorPrice) {
		this.errorPrice = errorPrice;
	}

	public String getConfirmCreation() {
		return confirmCreation;
	}

	public void setConfirmCreation(String confirmCreation) {
		this.confirmCreation = confirmCreation;
	}

	public String getErrorDelete() {
		return errorDelete;
	}

	public void setErrorDelete(String errorDelete) {
		this.errorDelete = errorDelete;
	}

	public String getErrorEdit() {
		return errorEdit;
	}

	public void setErrorEdit(String errorEdit) {
		this.errorEdit = errorEdit;
	}

	public boolean isEdit() {
		return isEdit;
	}

	public void setEdit(boolean isEdit) {
		this.isEdit = isEdit;
	}

	public boolean isShowError() {
		return isShowError;
	}

	public void setShowError(boolean isShowError) {
		this.isShowError = isShowError;
	}

}
